package ClassData.Visitor.MemoryMapper;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import ClassData.AttributeInfo;
import ClassData.AttributeInfoLineNumberTable;
import ClassData.AttributeInfo_Code;
import ClassData.AttributeInfo_ConstantValue;
import ClassData.AttributeInfo_Deprecated;
import ClassData.AttributeInfo_Exceptions;
import ClassData.AttributeInfo_InnerClasses;
import ClassData.AttributeInfo_LocalVariableTable;
import ClassData.AttributeInfo_Signature;
import ClassData.AttributeInfo_SourceFile;
import ClassData.AttributeInfo_Synthetic;
import ClassData.ClassData;
import ClassData.ClassDataList;
import ClassData.ConstantPool;
import ClassData.ConstantPoolClass_Info;
import ClassData.ConstantPoolDouble_Info;
import ClassData.ConstantPoolField_Info;
import ClassData.ConstantPoolFloat_Info;
import ClassData.ConstantPoolInfo;
import ClassData.ConstantPoolInteger_Info;
import ClassData.ConstantPoolLongValueDummy_Info;
import ClassData.ConstantPoolLong_Info;
import ClassData.ConstantPoolMethod_Info;
import ClassData.ConstantPoolName_Info;
import ClassData.ConstantPoolString_Info;
import ClassData.ConstantPoolUtf8_Info;
import ClassData.ExceptionHandler;
import ClassData.FieldInfo;
import ClassData.InterfaceInfo;
import ClassData.MethodInfo;
import ClassData.Visitor.IVisitor;

/**
 * Abstract class for MemoryMapper, 
 * which provides basic write functionality 
 * for file output purpose.  
 * @author Dominik Ollesz
 * @version 0.1
 */
public abstract class AbstractVisitor implements IVisitor {

	private boolean activated;
	protected String path;
	protected BufferedWriter output;
	
	public AbstractVisitor(String path) {
		if ((path == null) | (path == "")) {
			activated = false;
		} else {
			activated = true;
			open(path);
		}
	}
	
	/**
	 * Open output stream for writing.
	 */
	protected void open() {
		if (activated) {
			try {
				output = new BufferedWriter(new FileWriter(path));
			} catch (IOException fnf) {
				System.err.println("Could not open output stream for Memory Mapper");
				fnf.printStackTrace();
			};
		}
	}
	
	/**
	 * Open output stream for writing.
	 * @param path 
	 */
	protected void open(String path) {
		activated = true;
		try {
			this.path = path;
			output = new BufferedWriter(new FileWriter(path));
		} catch (IOException fnf) {
			System.err.println("Could not open output stream for Memory Mapper");
			fnf.printStackTrace();
		};
	}
	
	protected void write(String s) {
		if (activated) {
			try {
				output.write(s);
			} catch(IOException io) {
				System.err.println("MemoryMapper: could not write string into file");
				io.printStackTrace();
			}
		}
	}

	/**
	 * Close output stream.
	 */
	protected void close() {
		if (activated) {
			activated = false;
			try {
				output.close();
			} catch (IOException e) {
				System.err.println("Could not close output stream for Memory Mapper");
				e.printStackTrace();
			}
		}
	}

	// all Visitor Classes should implement their functionality in 
	// the following methods, which are part of the IVisitor interface.
	
	@Override
	public void visit_AttributeInfo(AttributeInfo myAttributeInfo) {
		// empty: no functionality
	}
	
	@Override
	public void visit_AttributeInfo_Code(AttributeInfo_Code myAttributeInfo_Code) {
		// empty: no functionality		
	}
	
	@Override
	public void visit_AttributeInfo_ConstantValue(
			AttributeInfo_ConstantValue myAttributeInfo_ConstantValue) {
		// empty: no functionality
	}
	
	@Override
	public void visit_AttributeInfo_Deprecated(
			AttributeInfo_Deprecated myAttributeInfo_Deprecated) {
		// empty: no functionality
	}
	
	@Override
	public void visit_AttributeInfo_Excetions(
			AttributeInfo_Exceptions myAttributeInfo_Exceptions) {
		// empty: no functionality
	}
	
	@Override
	public void visit_AttributeInfo_InnerClass(
			AttributeInfo_InnerClasses myAttributeInfo_InnerClasses) {
		// empty: no functionality
	}
	
	@Override
	public void visit_AttributeInfo_LocalVariableTable(
			AttributeInfo_LocalVariableTable myAttributeInfo_LocalVariableTable) {
		// empty: no functionality
	}
	
	@Override
	public void visit_AttributeInfo_Signature(
			AttributeInfo_Signature myAttributeInfo_Signature) {
		// empty: no functionality
	}
	
	@Override
	public void visit_AttributeInfo_SourceFile(
			AttributeInfo_SourceFile myAttributeInfo_SourceFile) {
		// empty: no functionality
	}
	
	@Override
	public void visit_AttributeInfo_Synthetic(
			AttributeInfo_Synthetic myAttributeInfo_Synthetic) {
		// empty: no functionality
	}
	
	@Override
	public void visit_AttributeLineNumberTable(
			AttributeInfoLineNumberTable myAttributeInfoLineNumberTable) {
		// empty: no functionality
	}
	
	@Override
	public void visit_ClassData(ClassData myClassData) {
		// empty: no functionality
	}
	
	@Override
	public void visit_ClassDataList(ClassDataList myClassDataList) {
		// empty: no functionality
	}
	
	@Override
	public void visit_ConstantPool(ConstantPool myConstantPool) {
		// empty: no functionality
	}
	
	@Override
	public void visit_ConstantPoolClass_Info(ConstantPoolClass_Info myClass_Info) {
		// empty: no functionality
	}
	
	@Override
	public void visit_ConstantPoolDouble_Info(
			ConstantPoolDouble_Info myConstantPoolDouble_Info) {
		// empty: no functionality
	}
	
	@Override
	public void visit_ConstantPoolField_Info(
			ConstantPoolField_Info myConstantPoolField_Info) {
		// empty: no functionality
	}
	
	@Override
	public void visit_ConstantPoolFloat_Info(
			ConstantPoolFloat_Info myConstantPoolFloat_Info) {
		// empty: no functionality
	}
	
	@Override
	public void visit_ConstantPoolInfo(ConstantPoolInfo myConstantPoolInfo) {
		// empty: no functionality
	}
	
	@Override
	public void visit_ConstantPoolInteger_Info(
			ConstantPoolInteger_Info myConstantPoolInteger_Info) {
		// empty: no functionality
	}
	
	@Override
	public void visit_ConstantPoolLong_Info(
			ConstantPoolLong_Info myConstantPoolLong_Info) {
		// empty: no functionality
	}
	
	@Override
	public void visit_ConstantPoolLongValueDummy_Info(
			ConstantPoolLongValueDummy_Info myConstantPoolLongValueDummy_Info) {
		// empty: no functionality
	}
	
	@Override
	public void visit_ConstantPoolMethod_Info(
			ConstantPoolMethod_Info myConstantPoolMethod_Info) {
		// empty: no functionality
	}
	
	@Override
	public void visit_ConstantPoolName_info(
			ConstantPoolName_Info myConstantPoolName_Info) {
		// empty: no functionality
	}
	
	@Override
	public void visit_ConstantPoolString_Info(
			ConstantPoolString_Info myConstantPoolString_Info) {
		// empty: no functionality
	}
	
	@Override
	public void visit_ConstantPoolUtf8_Info(
			ConstantPoolUtf8_Info myConstantPoolUtf8_Info) {
		// empty: no functionality
	}
	
	@Override
	public void visit_ExceptionHandler(ExceptionHandler myExceptionHandler) {
		// empty: no functionality
	}
	
	@Override
	public void visit_FieldInfo(FieldInfo myFieldInfo) {
		// empty: no functionality
	}
	
	@Override
	public void visit_InterfaceInfo(InterfaceInfo myInfo) {
		// empty: no functionality
	}
	
	@Override
	public void visit_MethodInfo(MethodInfo myInfo) {
		// empty: no functionality
	}
	
}
